
export function useDownload () {
  //下载
  const download = (headers: any, data: any) => {
    if (!data) {
      return;
    }

    //资源描述
    let contentDisposition = headers['content-disposition'];
    let pattern = new RegExp('filename=([^;]+\\.[^\\.;]+);*');
    let result = pattern.exec(contentDisposition);
    if (result !== null) {
      // 使用decodeURI对名字进行解码
      let fileName = decodeURI(result[1]);

      //创建链接标签
      let downloadElement = document.createElement('a');

      // 创建下载的链接
      let href = window.URL.createObjectURL(new Blob([data]));
      downloadElement.style.display = 'none';
      downloadElement.href = href;

      // 下载后文件名
      downloadElement.download = fileName;
      document.body.appendChild(downloadElement);

      // 点击下载
      downloadElement.click();

      // 下载完成移除元素
      document.body.removeChild(downloadElement);

      // 释放掉blob对象
      window.URL.revokeObjectURL(href);
    }
  }

  return {
    download
  }
}
